package com.wq.struct;

/**
 * @Author: wangqiang20995
 * @Date:2019/2/19
 * @Description:
 * @Resource:
 */
public class QueueStruct extends Struct {

    private int[] container;
    private int head;
    private int tail;
    private int length;

    public QueueStruct(int limit) {
        this.container = new int[limit];
        head = 0;
        tail = -1;
        length = 0;
    }


    public void enQueue(int element) {
        if (isFull()) {
            throw new RuntimeException("队列已满");
        }

        if (this.tail == this.container.length - 1) {
            this.tail = -1;
        }

        this.container[++tail] = element;
        this.length++;
    }

    public int deQueue() {
        if (isEmpty()) {
            throw new RuntimeException("队列已空");
        }

        if (this.head == this.container.length - 1) {
            this.head = 0;
        }

        int element = this.container[this.head++];
        this.length--;
        return element;
    }

    @Override
    protected void printStruct() {
        if (this.head > this.tail) {
            for (int i = this.head; i < this.container.length; i++) {
                System.out.printf("%d\t", this.container[i]);
            }
            for (int i = 0; i <= this.tail; i++) {
                System.out.printf("%d\t", this.container[i]);
            }

            System.out.printf("\n");
        } else {
            for (int i = this.head; i <= this.tail; i++) {
                System.out.printf("%d\t", this.container[i]);
            }
            System.out.printf("\n");
        }
    }

    private boolean isFull() {
        return this.length == this.container.length;
    }

    private boolean isEmpty() {
        return this.length == 0;
    }

    public static void main(String args[]) {
        QueueStruct queueStruct = new QueueStruct(10);
        queueStruct.enQueue(2);
        queueStruct.enQueue(3);
        queueStruct.enQueue(1);
        queueStruct.enQueue(4);
        queueStruct.enQueue(5);
        queueStruct.enQueue(6);
        queueStruct.enQueue(7);
        queueStruct.enQueue(8);
        queueStruct.enQueue(9);
        queueStruct.enQueue(91);
        queueStruct.printStruct();
        System.out.println("出队首元素-->" + queueStruct.deQueue());
        System.out.println("出队首元素-->" + queueStruct.deQueue());
        System.out.println("出队首元素-->" + queueStruct.deQueue());
        System.out.println("出队首元素-->" + queueStruct.deQueue());
        System.out.println("出队首元素-->" + queueStruct.deQueue());
        queueStruct.enQueue(30);
        queueStruct.printStruct();

    }
}
